<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Circle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\note;

use app\api\model\dao\Note;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Log;
use think\Validate;
use think\Exception;

class ApproveNote extends PageBase
{

    private $objNote;

    public function __construct()
    {
        parent::__construct();

        $this->objNote = new Note();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'note_id' => 'require|number',
            'status' => 'require|number|in:1,2',
        ];
        $msg = [
            'note_id.require' => '帖子id不能为空',
            'status.require' => '圈子审核状态不能为空',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            return $this->getRes(Error::ERR_NO_AUTH);
        }

        // 1、参数校验
        $ret = $this->checkParam($param);

        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        if (Flag::CONST_NOTE_APPROVE_REJECT == $param['status'] && empty($param['reason'])) {
            return $this->getRes(Error::ERR_PARAM, '请填写拒绝理由');
        }

        // 2、从数据库中获取圈子详情
        $ret = $this->objNote->getNoteByid($param['note_id']);
        if (false === $ret) {
            return $this->getRes(Error::ERR_SYS, Error::$arr_err_msg[Error::ERR_SYS] . 'get note info from db fail');
        }

        if (empty($ret)) {
            return $this->getRes(Error::ERR_NOTE_NOT_EXIST);
        }

        // 3、校验是否被删除
        if (Flag::CONST_NOTE_DELETED == $ret['is_delete']) {
            return $this->getRes(Error::ERR_NOTE_DELETED);
        }

        // 4、校验是否审核
        if (Flag::CONST_NOTE_APPROVE != $ret['approve_status']) {
            return $this->getRes(Error::ERR_NOTE_APPROVE);
        }

        $ret = $this->objNote->approveNote($param);
        if (false === $ret) {
            return $this->getRes(Error::ERR_SYS);
        }

        return $this->getRes();

    }
}
